//
//  CompanyMineIntegrityEmployerViewController.m
//  Btpin
//
//  Created by Vic on 2019/4/8.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyMineIntegrityEmployerViewController.h"
#import <WebKit/WebKit.h>

@interface CompanyMineIntegrityEmployerViewController ()<WKNavigationDelegate, WKUIDelegate>
{
    WKWebView *webView;
}
@property (nonatomic, strong) UIProgressView *progressView;

@end

@implementation CompanyMineIntegrityEmployerViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"诚信雇主";
    
    webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height)];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.btpin.com/h5/company/integrity_union?app_version=%@&token=%@", [UIToolsControl getVersionAndBuild], UD_GetOForK(isToken)]]]];
    webView.navigationDelegate = self;
    webView.UIDelegate = self;
    webView.allowsBackForwardNavigationGestures = YES;
    [self.view addSubview:webView];
    
    //TODO:kvo监听，获得页面title和加载进度值
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
    [webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
}
- (UIProgressView *)progressView {
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 2)];
        _progressView.progressTintColor = THEMECOLOR_OF_ORANGE;
        _progressView.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        _progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
        [self.view addSubview:self.progressView];
    }
    return _progressView;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        if (object == webView) {
            self.progressView.progress = webView.estimatedProgress;
            if (self.progressView.progress == 1) {
                __weak __typeof(self)weakSelf = self;
                [UIView animateWithDuration:0.5f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^ {
                    weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
                } completion:^(BOOL finished) {
                    weakSelf.progressView.hidden = YES;
                }];
            }
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    } else if ([keyPath isEqualToString:@"title"]) {
        if (object == webView) {
            self.navigationItem.title = webView.title;
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    self.progressView.hidden = NO;
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    [self.view bringSubviewToFront:self.progressView];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    self.progressView.hidden = YES;
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    if (error.code==NSURLErrorCancelled) {
        [self webView:webView didFinishNavigation:navigation];
    } else {
        self.progressView.hidden = YES;
    }
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    self.progressView.hidden = YES;
    SVPHUDError(@"加载失败");
}
#pragma mark 移除观察者
- (void)dealloc {
    [webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [webView removeObserver:self forKeyPath:@"title"];
}
- (void)customBackButtonTap {
    if (webView.canGoBack == YES) {
        [webView goBack];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
